//
//  HistoryConstructDetailController.m
//  Runone
//
//  Created by runone02 on 16/12/7.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "HistoryConstructDetailController.h"
#import "IncidentDetailCell.h"
#import "ROPointAnnotation.h"
#import "CustomAnnotationView.h"
#import "CustomCallOutView.h"
#import "RequestGinsengModel.h"
#import "RequestHelper.h"
#import "AppLocalManager.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "EventDetailModel.h"
#import "MJExtension.h"
#import "EventBaseInfoModel.h"
#import "EventDealListModel.h"
#import "SecetionHeaderView.h"
#import "CameraGunDetailViewController.h"
#import "RightCodeTool.h"
#import "EventPictureCell.h"
#import "EventCellFrame.h"
#import "SGImagePickerController.h"
#import "UIView+Addition.h"
#import "OneConst.h"
#import "CameraDetailController.h"
#import "OneConst.h"
#import "ROActionSheet.h"
#import "LittleVideoViewController.h"
#import "AppConstants.h"
#import "EnlargeMapViewController.h"
#import "CameraDetailController.h"
#import "HistoryConstrutView.h"

@interface HistoryConstructDetailController ()
@property (nonatomic, strong) HistoryConstrutView *headerView;//头部

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong)  EventDetailModel *deatilModel;

@property (nonatomic, strong) NSMutableArray *annotationsArray;

@property (nonatomic, strong) DeviceBaseInfo *minDistanceCamera;//最近的摄像枪model

@property (assign, nonatomic) AllIconType type;

@property (nonatomic, strong) ROActionSheet *sheet;

@property (nonatomic, strong) HighwayMergeRoadRecordModel *currentRoadModel; //当前事件的
@end

@implementation HistoryConstructDetailController

#pragma    ----------搜索界面 施工事件详情


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (_CEModel.IsRamp) {
        _type = AllIconTypeRamp;
    }else {
    _type = AllIconTypeConstruct;
    }
    
    _currentRoadModel = [[HighwayMergeRoadRecordModel alloc] init];
    _annotationsArray = [[NSMutableArray alloc] init];
    _deatilModel      = [[EventDetailModel alloc] init];
    _dataArray        = [[NSMutableArray alloc] init];
    
    [self loadConstructionData];
    [self configNavigationbar]; // 导航栏
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden            = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [_sheet hiddenSheet];
}

- (void) configNavigationbar {
#pragma mark - 屏蔽添加图片
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"camer_24.png"] style:UIBarButtonItemStylePlain target:self action:@selector(addPictureOrVideo)];
    self.view.backgroundColor=[UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.size     = CGSizeMake(100, 30);
    titleLabel.text     = @"历史事件详情";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
}


- (void)loadConstructionData { //施工事件
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetConstructionInfoModel=[[RequestGinsengModel alloc] init];
    GetConstructionInfoModel.URL         = GetEventDataURL;
    GetConstructionInfoModel.DataValue   = [self DataValueStr:self.CEModel.IncidentUID];
    GetConstructionInfoModel.UpdatedTime = @"";
    GetConstructionInfoModel.SystemCode  = @"GuangleHighway";
    GetConstructionInfoModel.MethodName  = @"GetConstructionEventDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
//        NSLog(@"%@",requestOutginseng.DataValue);
        
        _deatilModel = [EventDetailModel mj_objectWithKeyValues:requestOutginseng.DataValue];
        for (EventDealListModel *dealModel in _deatilModel.EventDealList) {
            EventCellFrame *cellF = [[EventCellFrame alloc] init];
            cellF.lModel = dealModel;
            [_dataArray addObject:cellF];
        }
        [self configTableView];
        [self configHeaderView];
        [self.tableView reloadData];
        
    };
    [requestHelper RequestGinsengModel:GetConstructionInfoModel andPushController:self];
}

- (void) configTableView {
    self.tableView.contentInset   = UIEdgeInsetsMake(64, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces        = NO;
    self.tableView.delegate       = self;
    self.tableView.dataSource     = self;
    //加载施工事件的头部
}

- (void) configHeaderView {
    CGFloat height = [self caculateHeaderViewHeight];
    _headerView            = [[[NSBundle mainBundle] loadNibNamed:@"HistoryConstrutView" owner:self options:nil] lastObject];
    _headerView.frame      = CGRectMake(0, 0, self.view.bounds.size.width,height);
    _headerView.ceModel    = _CEModel;
    _headerView.eventModel = _deatilModel;
    [_headerView.enlargeBtn addTarget:self action:@selector(enlargeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = _headerView;
}

#pragma mark -计算头部的高度
- (CGFloat) caculateHeaderViewHeight {
    CGFloat dealDetailH ;
    CGFloat unitsH;
    CGSize maxSize1   = CGSizeMake([UIScreen mainScreen].bounds.size.width - 80 - 16 - 8, MAXFLOAT);
    CGSize maxSize2   = CGSizeMake([UIScreen mainScreen].bounds.size.width - 16 - 8, MAXFLOAT);
    if (![_deatilModel.Content isEqualToString:@""]) {
        dealDetailH = [_deatilModel.Content boundingRectWithSize:maxSize1 options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
    }
    // 校准
    if (dealDetailH < 17) {
        dealDetailH = 35;
    }
    
    if (![_deatilModel.Department isEqualToString:@""]) {
        NSString *str      = @"施  工  单  位 :";
        NSString *unitsStr = [str stringByAppendingString:_deatilModel.Department];
        unitsH = [unitsStr boundingRectWithSize:maxSize2 options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
        if (unitsH < 17) {
            unitsH = 20;
        }
    }else {
        unitsH = 20;
    }

    
    CGFloat headerHeight = 200 + 160 + dealDetailH + 3 * 8 + 15 + unitsH;
    return headerHeight;
}

- (void) enlargeBtnClick {
    EnlargeMapViewController *vc = [[EnlargeMapViewController alloc] init];
    vc.la                        = [_CEModel.Latitude floatValue];
    vc.lo                        = [_CEModel.Longitude floatValue];
    vc.type                      = _type;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EventPictureCell *cell = [EventPictureCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    EventCellFrame *cellF = _dataArray[indexPath.row];
    cell.cellFrame = cellF;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EventCellFrame *cellF =  _dataArray[indexPath.row] ;
    return cellF.cellH;
}

- (NSString *)DataValueStr:(NSString *)IncidentUID {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"EventUID\",\"value\":\"%@\"}]",IncidentUID];
    return valueStr;
}

@end
